package launcher;

import sincro.basic.GeneraPaquete;
import sincro.basic.LauncherConf;
import sincro.basic.ListadoFicheros;
import sincro.basic.SincronizaFicheros;
import sincro.common.CommandExecuter;
import sincro.common.LocalUtil;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.JOptionPane;

import common.Connection;
import common.Constants;
import common.Display;
import common.PropertiesManager;
import creador.Creador;

public class Launcher {

	public static void main(String[] args) {
		new Launcher(args);
	}

	public Launcher(String[] args) {
		OptionManager om = new OptionManager(args);

		int error = om.checkArgs();
		if (error != OptionManager.OK) {
			switch (error) {
			case OptionManager.NO_ARGS:
			case OptionManager.NO_MODE:
				JOptionPane.showMessageDialog(null, "Uso: [-create|-sinc|-rl|-copy|-list] [opciones]");
				return;
			case OptionManager.RCMODE_NOARGS:
			case OptionManager.RCMODE_NORCDIR:
				JOptionPane.showMessageDialog(null, "Uso: [-sinc|-copy|-list] -d directorio_incidencia [-e entorno]");
				return;
			case OptionManager.NORCMODE_NOARGS:
			case OptionManager.NORCMODE_NOCOMMANDS:
				JOptionPane.showMessageDialog(null, "Uso: [-rl] [-chb|-chu|-ib|-iu|-cucust|-cucore] [-e entorno]");
				return;
			}
		}

		Display display = new Display();

		switch (om.getMode()) {

		case OptionManager.OP_CREATE:
			new Creador();
			break;

		case OptionManager.OP_LIST:
			if (!initRCMode(display, om))
				return;
			display.setVisible(true);
			Display.log("Inicio de listado de ficheros");
			Display.log("------------------------------------------------------------------------------------------------------------");
			new ListadoFicheros(PropertiesManager.getProperty("Local", "ProjectFilePath"), PropertiesManager.getProperty("Local", "DocDirPath"), PropertiesManager.getProperty("Local", "ListOutputFilename"));
			Display.log("------------------------------------------------------------------------------------------------------------");
			Display.log("Fin de listado de ficheros");
			return;

		case OptionManager.OP_COPY:
			if (!initRCMode(display, om))
				return;
			display.setVisible(true);
			Display.log("Inicio de copiado de ficheros");
			Display.log("------------------------------------------------------------------------------------------------------------");
			new GeneraPaquete();
			Display.log("------------------------------------------------------------------------------------------------------------");
			Display.log("Fin de copiado de ficheros");
			return;

		case OptionManager.OP_SINC:
			if (!initRCMode(display, om))
				return;
			Display.log("Inicio de sincronizacion de ficheros");
			Display.log("------------------------------------------------------------------------------------------------------------");
			LauncherConf lc = new LauncherConf(LocalUtil.readFileList(PropertiesManager.getProperty("Local", "ProjectFilePath")), LocalUtil.readLines(PropertiesManager.getProperty("Local", "SelectedFilesPath")), display);
			if (lc.isCanceled()) {
				System.exit(0);
			} else {
				Connection conn = new Connection(om.getEnvironment());
				display.setVisible(true);
				new SincronizaFicheros(conn, lc.getComp(), lc.getCheck(), lc.getItems());
				LocalUtil.writeLines(PropertiesManager.getProperty("Local", "SelectedFilesPath"), lc.getItems());
			}
			Display.log("------------------------------------------------------------------------------------------------------------");
			Display.log("Fin de sincronizacion de ficheros");
			return;

		case OptionManager.OP_RL:
			runNoRCMode(display, om);
			return;
		}
	}

	private boolean initRCMode(Display display, OptionManager om) {

		String directorioIncidendia = om.getRcDir();
		
		File di = new File(directorioIncidendia);
		if (!di.exists() || !di.isDirectory() || !di.canWrite()) {
			Display.log("No se puede usar el directorio raiz '" + directorioIncidendia + "'");
			return false;
		}

		try {
			directorioIncidendia = di.getCanonicalPath().replace("\\", "/") + "/";
		} catch (IOException e) {
			Display.log("Directorio raiz incorrecto");
			return false;
		}

		Display.log("Directorio raiz: " + directorioIncidendia);

		String prjFileName = LocalUtil.getProjectFileName(di);
		if (prjFileName == null) {
			Display.log("Fichero de proyecto no encontrado");
			return false;
		}

		PropertiesManager.setProperty("Local", "ProjectFilePath", directorioIncidendia + prjFileName);
		PropertiesManager.setProperty("Local", "ImpDirPath", directorioIncidendia + Constants.IMPDIR_NAME);
		PropertiesManager.setProperty("Local", "DocDirPath", directorioIncidendia + Constants.DOCDIR_NAME);
		PropertiesManager.setProperty("Local", "SelectedFilesPath", directorioIncidendia + "Scripts/" + Constants.SELECTED_FILES_NAME);

		return true;
	}

	public void runNoRCMode(Display display, OptionManager om) {

		String environment = om.getEnvironment();
		
		Connection conn = new Connection(environment);
		conn.initSession();

		List<String> commandList = om.getRemoteLauncherCommand();

		display.setVisible(true);
		
		if (commandList.contains("cucust")) {
			Display.log("Inicio del compilado");
			Display.log("------------------------------------------------------------------------------------------------------------");
			new CommandExecuter(conn, PropertiesManager.getProperty(environment, "compileUpstreamCommand"), true);
			Display.log("------------------------------------------------------------------------------------------------------------");
			Display.log("Fin del compilado");
		}

		if (commandList.contains("cucore")) {
			Display.log("Inicio del compilado");
			Display.log("------------------------------------------------------------------------------------------------------------");
			new CommandExecuter(conn, PropertiesManager.getProperty(environment, "compileUpstreamCoreCommand"), true);
			Display.log("------------------------------------------------------------------------------------------------------------");
			Display.log("Fin del compilado");
		}

		if (commandList.contains("chu")) {
			Display.log("Inicio de checkmeta");
			Display.log("------------------------------------------------------------------------------------------------------------");
			new CommandExecuter(conn, PropertiesManager.getProperty(environment, "upstreamcommand"));
			Display.log("------------------------------------------------------------------------------------------------------------");
			Display.log("Fin de checkmeta");
		}

		if (commandList.contains("chb")) {
			Display.log("Inicio de checkmeta");
			Display.log("------------------------------------------------------------------------------------------------------------");
			new CommandExecuter(conn, PropertiesManager.getProperty(environment, "buyercommand"));
			Display.log("------------------------------------------------------------------------------------------------------------");
			Display.log("Fin de checkmeta");
		}

		if (commandList.contains("iu")) {
			Display.log("Inicio de initdb");
			Display.log("------------------------------------------------------------------------------------------------------------");
			new CommandExecuter(conn, PropertiesManager.getProperty(environment, "initdbUpstreamCommand"));
			Display.log("------------------------------------------------------------------------------------------------------------");
			Display.log("Fin de initdb");
		}

		if (commandList.contains("ib")) {
			Display.log("Inicio de initdb");
			Display.log("------------------------------------------------------------------------------------------------------------");
			new CommandExecuter(conn, PropertiesManager.getProperty(environment, "initdbBuyerCommand"));
			Display.log("------------------------------------------------------------------------------------------------------------");
			Display.log("Fin de initdb");
		}

		conn.closeSession();
	}
}
